package com.pignest.hotel.ui.protocol;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.ProtocolList;
import com.pignest.hotel.beans.ProtocolPrice;
import com.pignest.hotel.beans.ProtocolUnit;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class ProtocolUnitVbox extends Vbox implements EventListener {

	/**
	 * 协议单位管理列表
	 */
	private static final long serialVersionUID = 8321265484286150046L;

	private Textbox qNo;
	
	private Textbox qCompany;
	
	private Textbox qName;
	
	private Listbox qIshangUp;
	
	private Listbox protocolUnitListbox;
	
	public Listbox priceListbox;
	
	private Listbox hangUpListbox;
	@Override
	public void onEvent(Event arg0) throws Exception {
		// TODO Auto-generated method stub

	}
	public void onCreate() {
		qNo =(Textbox)getFellow("qNo");
		qCompany=(Textbox)getFellow("qCompany");
		qName=(Textbox)getFellow("qName");
		qIshangUp=(Listbox)getFellow("qIshangUp");
		protocolUnitListbox=(Listbox)getFellow("protocolUnitListbox");
		priceListbox =(Listbox)getFellow("priceListbox");
		
		hangUpListbox=(Listbox)getFellow("hangUpListbox");
		query();
	}
	public void query(){
		protocolUnitListbox.getItems().clear();
		String no =qNo.getValue();
		String company =qCompany.getValue();
		String name =qName.getValue();
		Boolean isHangUp =null;
		String sHangUp =UIUtils.getSelectedValue(qIshangUp);
		if(sHangUp==null||sHangUp.equals(""))
			isHangUp=null;
		else if(sHangUp.equals("0"))
			isHangUp=false;
		else
			isHangUp=true;
		//System.out.println("hangUp is"+isHangUp.toString());
		List<ProtocolUnit> list =Services.getOtherBusiService().queryProtocolUnit(no, company, name,isHangUp);
		if(list==null)
			return ;
		int index=1;
		for(final ProtocolUnit p:list)
		{
			Listitem listitem = new Listitem();
			listitem.setValue(p.getId());
			listitem.setParent(protocolUnitListbox);
			listitem.appendChild(new Listcell(String.valueOf(index)));
			listitem.appendChild(new Listcell(p.getProtocolNo()));
			listitem.appendChild(new Listcell(p.getCompany()));
			listitem.appendChild(new Listcell(p.getContactName()));
			listitem.appendChild(new Listcell(p.getPhone()));
			listitem.appendChild(new Listcell(p.getCnIsHangUp()));
			listitem.appendChild(new Listcell(Utils.toPrice(p.getMaxPrice())));
			listitem.appendChild(new Listcell(Utils.toPrice(p.getPrice())));
			listitem.appendChild(new Listcell(p.getRemark()));
			listitem.addEventListener(Events.ON_CLICK, new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					// TODO Auto-generated 
					queryProtocolPrice(p.getId());
					queryHangUpList(p.getId());
				}
			});
			index++;
		}
	}
	public void queryProtocolPrice(String protocolId){
		priceListbox.getItems().clear();
		List<ProtocolPrice> list =Services.getOtherBusiService().queryProtocolPrice(protocolId);
		if(list==null)
			return ;
		int i=1;
		for(final ProtocolPrice p:list)
		{
			Listitem listitem = new Listitem();
			listitem.setValue(p.getId());
			listitem.setParent(priceListbox);
			listitem.appendChild(new Listcell(String.valueOf(i)));
			listitem.appendChild(new Listcell(p.getRoomType().getName()));
			listitem.appendChild(new Listcell(Utils.toPrice(p.getRoomType().getPrice())));
			Listcell cell = new Listcell();
			final Textbox price = new Textbox();
			price.setValue(Utils.toPrice(p.getPrice()));
			cell.appendChild(price);
			listitem.appendChild(cell);
			price.addEventListener(Events.ON_BLUR, new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					// TODO Auto-generated method stub
					edit(p.getId(),price.getValue());
				}
			});
			i++;
		}
	}
	public void queryHangUpList(String protocolId){
		hangUpListbox.getItems().clear();
		List<ProtocolList> list =Services.getOtherBusiService().queryProtocolList(protocolId, null);
		if(list==null)
			return ;
		int i=1;
		for(ProtocolList l:list)
		{
			Listitem listitem = new Listitem();
			listitem.setValue(l.getId());
			listitem.setParent(hangUpListbox);
			listitem.appendChild(new Listcell(String.valueOf(i)));
			listitem.appendChild(new Listcell(l.getRoomName()));
			listitem.appendChild(new Listcell(l.getName()));
			listitem.appendChild(new Listcell(l.getOpenName2()));
			listitem.appendChild(new Listcell(Utils.toPrice(l.getAdjustPrice())));
			listitem.appendChild(new Listcell(Utils.toPrice(l.getPrice())));
			listitem.appendChild(new Listcell(DateUtil.toStringHotel(l.getLivingTime())));
			listitem.appendChild(new Listcell(DateUtil.toStringHotel(l.getLeaveTime())));
			listitem.appendChild(new Listcell(l.getState2()));
			listitem.appendChild(new Listcell(DateUtil.toStringHotel(l.getOpTime())));
			listitem.appendChild(new Listcell(l.getOpUser()));
			i++;
		}
	}
	public void edit(String priceId,String price){
		if(!Utils.checkPrice(price)){
			UIUtils.alert("协议价必须为正数！");
			return ;
		}
		ProtocolPrice p = Services.getOtherBusiService().get(ProtocolPrice.class, priceId);
		p.setOpTime(new Date());
		p.setOpUser(Hotel.getCurrentUser().getName());
		p.setPrice(Utils.toPrice(price));
		Services.getOtherBusiService().update(p);
		
	}
}
